﻿package messageHandle;

/**
 * 消息中心
 * @author qykings
 *
 */
import model.login.LoginServer;
import model.register.RegisterSever;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;

import protocol.Iprotocol;
import protocol.login.Login_protocol;
import protocol.register.Register_protocol;
import config.SystemDefine;

public class MessageCenter {
	//
	static private MessageCenter instance;
	//
	private static int loginID;
	//
	private static int ChatID;
	//
	private static int registerID;
	//
	private static int prokerId;

	private static int MapWalk;

	static public MessageCenter GetInstance() {
		if (instance == null) {
			instance = new MessageCenter();

			initsType();
		}
		return instance;
	}

	private static void initsType() {
		// 登录
		loginID = SystemDefine.getInstance().getLOGIN_ID();

		// 聊天
		ChatID = SystemDefine.getInstance().getChatId();

		// 注册
		registerID = SystemDefine.getInstance().getRegister_ID();

		// /斗地主
		prokerId = SystemDefine.getInstance().getPokerID();

	}

	public void addMsg(ChannelBuffer buffer, Channel channe) {

		try {

			int type = buffer.readInt();
			Iprotocol protocol;
			try {

				if (type == loginID) {
			 

					protocol = new Login_protocol();
					protocol.read(buffer);

					LoginServer.getInstance().Login((Login_protocol)protocol, channe);
				} else if (type == ChatID) {
					// ChatServer.getInstance().addMsg(msg);
				} else if (type == registerID) {

					protocol = new Register_protocol();
					protocol.read(buffer);
					RegisterSever.getInstance().regiterHandle((Register_protocol)protocol, channe);
				} else if (type == prokerId) {
					// --斗地主
					// PokerServer.getInstance().MessageHandle(msg);
				} else if (type == MapWalk) {
					// MapServer.getInstance().addMsg(msg);
				}
				// else if (type == SystemDefine.getInstance().getPoker_ready())
				// {
				// PokerServer.getInstance().ready(msg);
				// } else if (type ==
				// SystemDefine.getInstance().getPoker_RobLead_Times()) {
				// PokerServer.getInstance().RobLead_Times_Handle(msg);
				// }

			} catch (Exception e) {
				System.out.println("-MessageCenter---HandlerMsg---error--" + e);

			}
			buffer.clear();
			buffer = null;

			// MessageVo vo = new MessageVo();
			// vo.setChannel(channel);
			// vo.setType(type);
			// vo.setContent(msg);
			// //
			// HandlerMsg(vo);
		} catch (Exception e) {

			System.out.println(e + "--MessageCenter---addMsg--error--");
		}
	}

	// end class
}
